package com.nbsaas.boot.contoller;

import com.nbsaas.boot.app.api.apis.AppApi;
import com.nbsaas.boot.app.api.domain.request.AppRequest;
import com.nbsaas.boot.app.api.domain.request.AppSearch;
import com.nbsaas.boot.app.api.domain.response.AppResponse;
import com.nbsaas.boot.app.api.domain.simple.AppSimple;
import com.nbsaas.boot.app.ext.apis.AppExtApi;
import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *  对外控制器
 */
@RequiresAuthentication
@RestController
@RequestMapping("/app")
public class AppController {


    @Resource
    private AppApi appApi;

    @Resource
    private AppExtApi appExtApi;


    @RequiresPermissions("app")
    @RequestMapping("/search")
    public PageResponse<AppSimple> search(@RequestBody AppSearch request) {
        return appApi.search(request);
    }

    @RequiresPermissions("app")
    @RequestMapping("/list")
    public ListResponse<AppSimple> list(@RequestBody AppSearch request) {
        return appApi.list(request);
    }

    /**
     * 添加数据
     *
     * @param request
     * @return
     */
    @RequiresPermissions("app")
    @CreateData
    @RequestMapping("/create")
    public ResponseObject<AppResponse> create(@RequestBody @Validated(AddOperator.class) AppRequest request) {
        return appExtApi.create(request);
    }

    @RequiresPermissions("app")
    @UpdateData
    @RequestMapping("/update")
    public ResponseObject<AppResponse> update(@RequestBody @Validated(UpdateOperator.class) AppRequest request) {
        return appExtApi.update(request);
    }

    @RequiresPermissions("app")
    @RequestMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) AppRequest request) {
        return appApi.delete(request);
    }

    @RequiresPermissions("app")
    @RequestMapping("/view")
    public ResponseObject<AppResponse> view(@RequestBody @Validated(ViewOperator.class) AppRequest request) {
        return appApi.view(request);
    }
}